package cn.edu.zjut.mina.xcmahjong;

import java.util.ArrayList;
import java.util.List;

import org.apache.mina.core.session.IoSession;

import cn.edu.zjut.mina.bean.RoomInfo_XCMJ;
import cn.edu.zjut.mina.bean.StandardFormat;
import cn.edu.zjut.mina.bean.PlayerInfo;
import cn.edu.zjut.mina.interf.ICardBean;
import cn.edu.zjut.mina.interf.IGame;
import cn.edu.zjut.mina.protocal.JMessageProtocalRes;
import cn.edu.zjut.mina.protocal.ProtocalRegulation;
import cn.edu.zjut.mina.util.GsonUtils;

public class XcmjGame implements IGame {
	//房间信息
	RoomInfo_XCMJ roomInfo = null;
	//牌堆
	XcmjCardPile cardPile = new XcmjCardPile();
	//4个玩家
	XcmjPlayer[] players = { new XcmjPlayer(), new XcmjPlayer(), new XcmjPlayer(), new XcmjPlayer() };
	//上一张牌
	XcmjCardBean lastCb = null;
	//当前玩家
	int currentPlayer = 0;
	//循环局数
	int roundTimes = 0;
	// 牌堆只有overSign张牌时结束
	int overSign = 14;
	//游戏结束标记
	boolean gameOver;
	//opnum可以操作的玩家数
	int opNum;
	//实际操作的wanjiashu用在列队里
	int aOpNum;
	// 4个玩家的操作
	StandardFormat []operations=new StandardFormat[4];
	//初始化
	public XcmjGame(RoomInfo_XCMJ roomInfo) {
		this.roomInfo = roomInfo;
	}
	//设置当前玩家
	public void setCurrentPlayer(int currentPlayer) {
		this.currentPlayer = currentPlayer;
	}

	public void setGameOver(boolean gameOver) {
		this.gameOver = gameOver;
	}
	
	//游戏数据初始化
	public void init() {
		// TODO Auto-generated method stub
		gameOver = false;
		currentPlayer = 0;
		roundTimes = 0;
		//牌堆初始化
		cardPile.init();
		//打出去的牌堆初始化
		//discardPile.init();
		for (int i = 0; i < 4; i++) {
			//设置session
			players[i].setSession(roomInfo.getUserSessions().get(i).session);
			//设置userId
			players[i].setUserId(roomInfo.getUserSessions().get(i).user.getUserId());
			//玩家数据初始化
			players[i].init();
			//设置玩家的游戏信息
			players[i].setGame(this);
			//设置玩家编号，同position
			players[i].setPosition(roomInfo.getUserSessions().get(i).user.getPosition());
		}
		// 发牌
		for (int i = 0; i < 4; i++) {
			for (int j = 1; j <= 13; j++) {
				XcmjCardBean cardBean = cardPile.getCard();
				cardBean.setOwner(players[i].getPosition());
				cardBean.setUserId(players[i].getUserId());
				players[i].addCard(cardBean);
			}
		}
		
		// 
		List<PlayerInfo> tempPlayers = new ArrayList<PlayerInfo>();

		for (int j = 0; j < 4; j++) {
			// 发送的信息放到playerInfo中
			PlayerInfo playerInfo = new PlayerInfo();
			playerInfo.userId = players[j].getUserId();
			playerInfo.position = players[j].getPosition();

			playerInfo.cardBeans = players[j].getCards();
			playerInfo.gangArea = players[j].getPgArea().getgArea();
			playerInfo.pengArea = players[j].getPgArea().getpArea();
			tempPlayers.add(playerInfo);
		}

		// 把牌传出去
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		outJson.result=true;
		outJson.gameType = 1;
		outJson.players = tempPlayers;
		res.setResultCode(ProtocalRegulation.GAMEINIT);
		String data = GsonUtils.beanToJson(outJson);
		System.out.println(data);
		
		res.setContent(data);
		sendToEveryone(res);
		currentPlayer = 0;
		players[0].getCard();

	}

	public int getRoundTimes() {
		return roundTimes;
	}

	public void hpgQueue(StandardFormat inJson,IoSession session){
		aOpNum++;
		operations[inJson.position]=inJson;
		if(aOpNum==opNum){
			for(int i=1;i<4;i++){
				StandardFormat json=operations[(currentPlayer+i)%4];
				if(json!=null&&json.operation!=3){
					switch(json.operation){
					case 0:
						players[json.position].doPeng(inJson, session);
						printAllPlayer();
						return;
					case 1:
						players[json.position].doGang(inJson, session);
						printAllPlayer();
						return;
					case 2:
						players[json.position].doHu(inJson, session);
						printAllPlayer();
						return;
					}
				}
				if(i==3)
					players[(currentPlayer + 1) % 4].getCard();
			}
		}
			
	}

	// 进行一轮

//	public void doRound(StandardFormat inJson,IoSession session) {
//		// TODO Auto-generated method stub
//
//		while (players[currentPlayer].isDraw()) {
//			XcmjCardBean cb = players[currentPlayer].getCard();
//			players[currentPlayer].listenHPG(cb);
//		}
//		
//		// System.out.println("doRound:玩家"+currentPlayer);
//	}

	public void doRoundContinue(StandardFormat inJson,IoSession session) {
		players[currentPlayer].doDiscard(inJson,session);
	}
	
	
	public void nextPlayer(StandardFormat inJson,IoSession session){
		currentPlayer = (currentPlayer + 1) % 4;
	//	pauseOther(currentPlayer);
	}
	
	//发牌
	public void sendToEveryone(JMessageProtocalRes res) {
		for (int i = 0; i < players.length; i++) {
			players[i].getSession().write(res);
		}
	}
	//发送消息给其他玩家
	public void sendToOthers(JMessageProtocalRes res, int number) {
		for (int i = 0; i < players.length; i++) {
			if (i != number)
				players[i].getSession().write(res);
		}
	}
	
	public void printAllPlayer(){
		for (int i = 0; i < 4; i++) {
			XcmjPlayer player = players[i];
			System.out.print("玩家" + player.getUserId() + "的手牌如下:");
			StringBuffer sb = new StringBuffer();
			List<ArrayList<ICardBean>> handCards = player.getHandCards();
			for (int j = 0; j < handCards.size(); j++) {
				for (int k = 0; k < handCards.get(j).size(); k++) {
					sb.append(handCards.get(j).get(k).getName() + handCards.get(j).get(k).getIndex() + "  ");
				}
			}
			
			System.out.println(sb.toString());
			XcmjPGArea pgArea = player.getPgArea();
			System.out.print("玩家" + player.getUserId() + "的碰杠区牌如下:");
			sb = new StringBuffer();
			for (int j = 0; j < pgArea.getpArea().size(); j++) {
				sb.append(pgArea.getpArea().get(j).getName() + pgArea.getpArea().get(j).getIndex() + "  ");
			}
			for (int j = 0; j < pgArea.getgArea().size(); j++) {
				sb.append(pgArea.getgArea().get(j).getName() + pgArea.getgArea().get(j).getIndex() + "  ");
			}
			System.out.println(sb.toString());
		}
	}

}
